package com.ims.core.doc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ims.common.annotation.RequestParamsSplit;
import com.ims.core.vo.DeductionCancelnonApplyRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ims.common.annotation.Log;
import com.ims.common.core.controller.BaseController;
import com.ims.common.core.domain.AjaxResult;
import com.ims.common.enums.BusinessType;
import com.ims.core.doc.domain.TInInvoice;
import com.ims.core.doc.service.ITInInvoiceService;
import com.ims.common.utils.poi.ExcelUtil;
import com.ims.common.core.page.TableDataInfo;

/**
 * 进项发票Controller
 *
 * @author IMS
 * @date 2024-05-20
 */
@RestController
@RequestMapping("/doc/inInvoice")
public class TInInvoiceController extends BaseController
{
    @Autowired
    private ITInInvoiceService tInInvoiceService;

    /**
     * 查询进项发票列表
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(TInInvoice tInInvoice)
    {
        startPage();
        List<TInInvoice> list = tInInvoiceService.selectTInInvoiceList(tInInvoice);
        return getDataTable(list);
    }

    /**
     * 查询进项发票列表
     */
    @PreAuthorize("@ss.hasPermi('doc:listDeduction:list')")
    @GetMapping("/listDeduction")
    public TableDataInfo listDeduction(TInInvoice tInInvoice)
    {
        startPage();
        List<TInInvoice> list = tInInvoiceService.selectDeductionInInvoiceList(tInInvoice);
        return getDataTable(list);
    }

    /**
     * 根据税期查询查询进项发票列表
     */
    @PreAuthorize("@ss.hasPermi('doc:listDeduction:list')")
    @GetMapping("/invoiceListByPeriod/{period}")
    public AjaxResult invoiceListByPeriod(@PathVariable("period") String period)
    {
        List<TInInvoice> list = tInInvoiceService.selectDeductionInInvoiceListByPeriod(period);
        return success(list);
    }

    /**
     * 导出进项发票列表
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:export')")
    @Log(title = "进项发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TInInvoice tInInvoice)
    {
        List<TInInvoice> list = tInInvoiceService.selectTInInvoiceList(tInInvoice);
        ExcelUtil<TInInvoice> util = new ExcelUtil<TInInvoice>(TInInvoice.class);
        util.exportExcel(response, list, "进项发票数据");
    }

    /**
     * 获取进项发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:query')")
    @GetMapping(value = "/{invoiceId}")
    public AjaxResult getInfo(@PathVariable("invoiceId") String invoiceId)
    {
        return success(tInInvoiceService.selectTInInvoiceByInvoiceId(invoiceId));
    }

    /**
     * 新增进项发票
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:add')")
    @Log(title = "进项发票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TInInvoice tInInvoice)
    {
        return toAjax(tInInvoiceService.insertTInInvoice(tInInvoice));
    }

    /**
     * 修改进项发票
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:edit')")
    @Log(title = "进项发票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TInInvoice tInInvoice)
    {
        return toAjax(tInInvoiceService.updateTInInvoice(tInInvoice));
    }

    /**
     * 删除进项发票
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:remove')")
    @Log(title = "进项发票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{invoiceIds}")
    public AjaxResult remove(@PathVariable String[] invoiceIds)
    {
        return toAjax(tInInvoiceService.deleteTInInvoiceByInvoiceIds(invoiceIds));
    }

    /**
     * 进项发票勾选抵扣
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:deductionApply')")
    @Log(title = "进项发票勾选抵扣", businessType = BusinessType.DELETE)
	@GetMapping("/deductionApply/{invoiceIds}")
    public AjaxResult deductionApply(@PathVariable String[] invoiceIds)
    {
        tInInvoiceService.deductionApplyByInvoiceIds(invoiceIds);
        return success();
    }

    /**
     * 进项发票取消勾选抵扣
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:deductionApplyCancel')")
    @Log(title = "进项发票取消勾选抵扣", businessType = BusinessType.DELETE)
	@GetMapping("/deductionApplyCancel/{invoiceIds}")
    public AjaxResult deductionApplyCancel(@PathVariable String[] invoiceIds)
    {
        tInInvoiceService.deductionApplyCancelByInvoiceIds(invoiceIds);
        return success();
    }

    /**
     * 进项发票勾选不抵扣
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:deductionCancelnonApply')")
    @Log(title = "进项发票勾选不抵扣", businessType = BusinessType.DELETE)
    @GetMapping("/deductionCancelnonApply")
    public AjaxResult deductionCancelnonApply(DeductionCancelnonApplyRequest deductionCancelnonApplyRequest)
    {
        tInInvoiceService.deductionCancelnonApplyByInvoiceIds(deductionCancelnonApplyRequest.getInvoiceIds(), deductionCancelnonApplyRequest.getReason(), deductionCancelnonApplyRequest.getReasonRemark());
        return success();
    }

    /**
     * 进项发票勾选不抵扣
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:deductionCancelnonApplyCance')")
    @Log(title = "进项发票取消勾选不抵扣", businessType = BusinessType.DELETE)
    @GetMapping("/deductionCancelnonApplyCancel/{invoiceIds}")
    public AjaxResult deductionCancelnonApplyCancel(@PathVariable String[] invoiceIds)
    {
        tInInvoiceService.deductionCancelnonApplyCancelByInvoiceIds(invoiceIds, null, null);
        return success();
    }
}
